/*
 *
 */

jsx3.lang.Package.definePackage("tibco.admin.enterpriseAssets.shdResDefs", function (shdResDefs) {
});

jsx3.Class.defineClass(
        "tibco.admin.enterpriseAssets.shdResDefs.ShdResDefPicker",
        jsx3.lang.Object,
        null,
        function (picker) {

            picker.ONOK = "tibco.admin.enterpriseAssets.shdResDefs.PICKEROK";
            picker.ONSELECT = "tibco.admin.enterpriseAssets.shdResDefs.PICKERSELECT";

            picker.name = null;
            picker.filter = null;
            picker.dialog = null;

            picker.pickChoice = function(source, filter, name) {
                picker.source = source;
                picker.name = name;
                picker.filter = filter;

                picker.dialog = tibco.admin.Main.loadUIResource(tibco.admin.Main.getServer().getBodyBlock(),
                        "tibco.admin", "tibco/admin/enterpriseAssets/shdResDefs/components/shdResDefPicker.xml", true);

                // set the caption bar
                var server = tibco.admin.Main.getServer();
                picker.dialog.getFirstChildOfType("jsx3.gui.WindowBar").setText(server.getDynamicProperty("@wbarPicker_25_Text", filter),true);
                var mtxShdResDefList_25 = picker.dialog.getDescendantOfName("mtxShdResDefList_25");
				mtxShdResDefList_25.subscribe(picker.ONSELECT, picker.onSelect);
				
				//set the specific empty matrix messsage if there is one defined (1-8TQCYU)
				var strNullListMessage = server.getDynamicProperty("@mtxShdResDefList_25_NullListMessage_" + filter);
				if (strNullListMessage)
					mtxShdResDefList_25.setNullListMessage(strNullListMessage);
				
                picker.dialog.getDescendantOfName("btnOK_25").subscribe(picker.ONOK, picker.onOK);

                picker.callGetConfigurationsInEnterprise(filter);
            }

            picker.onSelect = function() {
                picker.dialog.getDescendantOfName("btnOK_25").setEnabled(jsx3.gui.Form.STATEENABLED, true);
                picker.dialog.getDescendantOfName("mtxShdResDefList_25").unsubscribe(picker.ONSELECT);
            }

            picker.onOK = function() {
                var value = picker.dialog.getDescendantOfName("mtxShdResDefList_25").getSelectedNodes().get(0).getAttribute("name");
                picker.source.setDynamicProperty("jsxtext", null);
                picker.source.setText(value, true);
                picker.dialog.doClose();

				//temp implementation, need to enhance(maybe with pubsub.). 070818
				var bodyBlock = tibco.admin.Main.getServer().getBodyBlock();//070818
				if(bodyBlock.getDescendantOfName("blkDetailsForType_25F")) bodyBlock.getDescendantOfName("blkDetailsForType_25F").handler.valiEMSJNDIName(bodyBlock.getDescendantOfName("btnJndiConfigName_25F"));//070818
				if(bodyBlock.getDescendantOfName("blkDetailsForType_25C")) bodyBlock.getDescendantOfName("blkDetailsForType_25C").handler.valiJDBCJNDIName(bodyBlock.getDescendantOfName("btnJndiConfigName_25C"));//070818
            }


            // WSDL operations
            // TODO: error handling
            // WSDL operation - getResourceConfigsInEnvironment
            picker.callGetConfigurationsInEnterprise = function(filter) {
                var objService = tibco.admin.Main.loadService("tibco.admin",
                        "tibco/admin/enterpriseAssets/shdResDefs/rules/getFilteredResourceConfigurationsInEnterprise.xml",
                        "getResourceConfigurationsInEnterprise", "Get Filtered Shared Resources Configurations")
                objService.setEndpointURL(tibco.admin.util.ServiceHelper.getSharedResourcesEndpointURL());

                objService.filter = filter;
                //subscribe and call
                objService.subscribe(jsx3.net.Service.ON_SUCCESS, picker.onGetResourceConfigurationsInEnterpriseSuccess);
                objService.subscribe(jsx3.net.Service.ON_ERROR, picker.onGetResourceConfigurationsInEnterpriseError);
                objService.subscribe(jsx3.net.Service.ON_INVALID, picker.onGetResourceConfigurationsInEnterpriseInvalid);
                objService.doCall();
            };

            picker.onGetResourceConfigurationsInEnterpriseSuccess = function(objEvent) {
                var matrix = picker.dialog.getDescendantOfName("mtxShdResDefList_25");
                if (picker.name) {
                    var record = matrix.getXML().selectSingleNode("//record[@name='" + picker.name + "']");
                    if (record) {
                        record.setAttribute("jsxselected", 1);
                        picker.onSelect();
                    }
                }
                matrix.repaintData();
            };

            picker.onGetResourceConfigurationsInEnterpriseError = function(objEvent) {
            };

            picker.onGetResourceConfigurationsInEnterpriseInvalid = function(objEvent) {
            };
            // End WSDL operations
        });
